package com.ruoyi.home.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.home.domain.HomepageTemplate;
import com.ruoyi.home.service.IHomepageTemplateService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 首页模板Controller
 * 
 * @author ruoyi
 * @date 2024-01-20
 */
@RestController
@RequestMapping("/home/template")
public class HomepageTemplateController extends BaseController
{
    @Autowired
    private IHomepageTemplateService homepageTemplateService;

    /**
     * 查询首页模板列表
     */
    @GetMapping("/list")
    public TableDataInfo list(HomepageTemplate homepageTemplate)
    {
        startPage();
        List<HomepageTemplate> list = homepageTemplateService.selectHomepageTemplateList(homepageTemplate);
        return getDataTable(list);
    }

    /**
     * 导出首页模板列表
     */
    @PreAuthorize("@ss.hasPermi('home:template:export')")
    @Log(title = "首页模板", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HomepageTemplate homepageTemplate)
    {
        List<HomepageTemplate> list = homepageTemplateService.selectHomepageTemplateList(homepageTemplate);
        ExcelUtil<HomepageTemplate> util = new ExcelUtil<HomepageTemplate>(HomepageTemplate.class);
        util.exportExcel(response, list, "首页模板数据");
    }

    /**
     * 获取首页模板详细信息
     */
    @PreAuthorize("@ss.hasPermi('home:template:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(homepageTemplateService.selectHomepageTemplateById(id));
    }

    /**
     * 新增首页模板
     */
    @Log(title = "首页模板", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HomepageTemplate homepageTemplate)
    {
        // 新增模板默认不激活
        homepageTemplate.setIsActive(0);
        return toAjax(homepageTemplateService.insertHomepageTemplate(homepageTemplate));
    }

    /**
     * 修改首页模板
     */
    @Log(title = "首页模板", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HomepageTemplate homepageTemplate)
    {
        return toAjax(homepageTemplateService.updateHomepageTemplate(homepageTemplate));
    }

    /**
     * 删除首页模板
     */
    @Log(title = "首页模板", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(homepageTemplateService.deleteHomepageTemplateByIds(ids));
    }

    /**
     * 激活模板
     */
    @Log(title = "激活首页模板", businessType = BusinessType.UPDATE)
    @PutMapping("/activate/{id}")
    public AjaxResult activate(@PathVariable("id") Long id, @RequestParam("campusId") Long campusId)
    {
        return toAjax(homepageTemplateService.activateTemplate(id, campusId));
    }

    /**
     * 复制模板
     */
    @Log(title = "复制首页模板", businessType = BusinessType.INSERT)
    @PostMapping("/copy/{id}")
    public AjaxResult copy(@PathVariable("id") Long id, @RequestParam("templateName") String templateName)
    {
        return toAjax(homepageTemplateService.copyTemplate(id, templateName));
    }

    /**
     * 获取指定校区的启用模板
     */
    @GetMapping("/active/{campusId}")
    public AjaxResult getActiveTemplate(@PathVariable("campusId") Long campusId)
    {
        HomepageTemplate activeTemplate = homepageTemplateService.selectActiveTemplateByCampusId(campusId);
        return AjaxResult.success(activeTemplate);
    }
} 